package com.ruoyi.hd.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hd.domain.HdCommodityTime;
import com.ruoyi.hd.service.IHdCommodityTimeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hd.domain.HdCommodity;
import com.ruoyi.hd.service.IHdCommodityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品管理Controller
 * 
 * @author ruoyi
 * @date 2022-05-29
 */
@RestController
@RequestMapping("/hd/commodity")
public class HdCommodityController extends BaseController
{
    @Autowired
    private IHdCommodityService hdCommodityService;
    @Autowired
    private IHdCommodityTimeService hdCommodityTimeService;

    /**
     * 查询商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('hd:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(HdCommodity hdCommodity)
    {
        startPage();
        List<HdCommodity> list = hdCommodityService.selectHdCommodityList(hdCommodity);
        list.forEach(item->{
            Double price1 = Double.parseDouble(item.getPrice().toString());
            Double price = Double.parseDouble(item.getCommodityTime().getPrice().toString());
            item.setNewPrice(new BigDecimal((price - price1) / price).setScale(2,BigDecimal.ROUND_HALF_UP));
        });
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('hd:commodity:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HdCommodity hdCommodity)
    {
        List<HdCommodity> list = hdCommodityService.selectHdCommodityList(hdCommodity);
        ExcelUtil<HdCommodity> util = new ExcelUtil<HdCommodity>(HdCommodity.class);
        util.exportExcel(response, list, "商品管理数据");
    }

    /**
     * 获取商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('hd:commodity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hdCommodityService.selectHdCommodityById(id));
    }

    /**
     * 新增商品管理
     */
    @PreAuthorize("@ss.hasPermi('hd:commodity:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HdCommodity hdCommodity)
    {
        hdCommodity.setCreateBy(getUsername());
        return toAjax(hdCommodityService.insertHdCommodity(hdCommodity));
    }

    /**
     * 修改商品管理
     */
    @PreAuthorize("@ss.hasPermi('hd:commodity:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HdCommodity hdCommodity)
    {
        hdCommodity.setUpdateBy(getUsername());
        return toAjax(hdCommodityService.updateHdCommodity(hdCommodity));
    }

    /**
     * 删除商品管理
     */
    @PreAuthorize("@ss.hasPermi('hd:commodity:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hdCommodityService.deleteHdCommodityByIds(ids));
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<HdCommodity> util = new ExcelUtil<HdCommodity>(HdCommodity.class);
        util.importTemplateExcel(response, "商品数据");
    }

    @Log(title = "商品管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('hd:commodity:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<HdCommodity> util = new ExcelUtil<HdCommodity>(HdCommodity.class);
        List<HdCommodity> commodityList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = hdCommodityService.importHdCommodity(commodityList, updateSupport, operName);
        return AjaxResult.success(message);
    }
}
